package com.yanxin.credit.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.yanxin.credit.dto.CovertCreditDTO;
import com.yanxin.credit.dto.MemberDTO;
import com.yanxin.credit.dto.PageDataInfo;
import com.yanxin.credit.entity.Goods;
import com.yanxin.credit.entity.Member;
import com.yanxin.credit.entity.MemberCreditRecord;
import com.yanxin.credit.mapper.MemberMapper;
import com.yanxin.credit.service.IGoodsService;
import com.yanxin.credit.service.IMemberCreditRecordService;
import com.yanxin.credit.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program spring-cloud-example
 * @description:
 * @author: LiuYanXin
 * @create: 2022-02-08 18:38
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private IMemberCreditRecordService memberCreditRecordService;


    public List<Member> selectList(String phone) {

        QueryWrapper<Member> query = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(phone)) {
            query.eq("phone", phone);
        }

        return memberMapper.selectList(query);
    }

    @Override
    public PageDataInfo listBySelective(String phone, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<Member> query = new QueryWrapper<>();
        query.orderByDesc("create_time");
        if (StrUtil.isNotEmpty(phone)) {
            query.eq("phone", phone).or()
                    .eq("id_card", phone);
        }
        List<Member> members = memberMapper.selectList(query);
        return new PageDataInfo(members);
    }

    @Override
    public int insertMember(Member member) {

        if (ObjectUtil.isEmpty(member.getMemberId())) {
            // 根据手机号或身份证查询该用户是否存在
            QueryWrapper<Member> query = new QueryWrapper<>();
            query.eq("phone", member.getPhone()).or().
                    eq("id_card", member.getIdCard());
            List<Member> members = memberMapper.selectList(query);
            if (CollUtil.isNotEmpty(members)) {
                throw new IllegalStateException("会员已存在,无须重新添加");
            }
            member.setCreateTime(new Date());
            member.setUpdateTime(new Date());
            // 名称加手机号后四位
            member.setName(member.getName() + StrUtil.subSuf(member.getPhone(), 7));
            return memberMapper.insert(member);
        } else {
            member.setUpdateTime(new Date());
            return memberMapper.updateById(member);
        }
    }

    @Override
    public int deleteMember(Member member) {
        return memberMapper.deleteById(member.getMemberId());
    }

    @Override
    public int addCredit(MemberDTO member) {
        // 记录当次变更积分数,然后统计用户积分
        MemberCreditRecord memberCreditRecord = new MemberCreditRecord();
        memberCreditRecord.setCredit(member.getCredit());
        memberCreditRecord.setMemberId(member.getMemberId());
        memberCreditRecord.setCreateUser(1);
        memberCreditRecord.setCreateTime(new Date());
        memberCreditRecord.setUpdateTime(new Date());
        memberCreditRecordService.insertMemberCreditRecord(memberCreditRecord);
        // 更新用户积分数
        Member memberUpdate = memberMapper.selectById(member.getMemberId());
        memberUpdate.setCredit(memberUpdate.getCredit().add(member.getCredit()));
        return memberMapper.updateById(memberUpdate);
    }

    @Override
    public int covertCreditToGoods(CovertCreditDTO covertCreditDTO) {
        // 查询用户积分
        Member member = memberMapper.selectById(covertCreditDTO.getMemberId());
        Goods goods = goodsService.getById(covertCreditDTO.getGoodsId());
        if (goods.getStock() < 1) {
            throw new IllegalArgumentException("当前商品库存不足,无法兑换");
        }
        if (member.getCredit().compareTo(goods.getCredit()) < 0) {
            throw new IllegalArgumentException("当前用户积分不足,无法兑换该商品");
        }
        // 更新用户积分
        member.setCredit(member.getCredit().subtract(goods.getCredit()));
        // 库存减1
        goods.setStock(goods.getStock() - 1);
        // TODO 使用乐观锁进行更新
        goodsService.updateById(goods);
        return memberMapper.updateById(member);
    }

    @Override
    public int updateMember(Member member) {
        return 0;
    }
}
